<style>
    .cartonPage-title-info{
        background:#FFFEE6;
        color:#8F5700;
        padding:12px;
    }
    .cartonPage-item-info{
        background:#f3f3f3;
        color:#8F5700;
        padding:12px;
    }
</style>
<div id="cartonPage_from" style="text-align: center">
    <!--公司按钮-->
    <div class="cartonPage-title-info">
        <div class="demo-tip icon-tip"></div>
        <div>
            公司名称:
            <input id="cartonPageCompanyNameCombobox" class="easyui-combobox"  data-options="valueField:'companyName',textField:'companyName'" />
            <span style="display: none">
                报价单号:
                <input id="cartonPageQuotationCodeCombobox" class="easyui-combobox"  data-options="valueField:'id',textField:'quotationCode'" />
            </span>
            <a id="cartonPageCompanyInfoManager" class="easyui-linkbutton c1">公司信息管理</a>
<!--            <a id="cartonPageCompanyDataManager" class="easyui-linkbutton c7" >公司数据管理</a>-->
        </div>
    </div>

</div>

<!--公司信息dialog-->
<div id="cartonPage_company_dialog"></div>

<!--公司信息操作内容-->
<div style='display: none' id='cartonPageCompanyOperation'>
    <a class='easyui-linkbutton c1' onclick="cartonPageCompanyAdd()">添加公司信息</a>
    <a class='easyui-linkbutton c2' onclick="cartonPageCompanyUpdate()">修改公司信息</a>
    <a class='easyui-linkbutton c3' onclick="cartonPageCompanyDelete()">删除公司信息</a>
</div>
<!--表数据-->
<div style="margin: 0 auto">
    <table id="cartonPageTable" toolbar="#cartonPageTable_tb"></table>
<!--    备注-->
    <label id="labelRemark"></label><br>
    <!-- 说明-->
    <label id="labelExplain"></label>
</div>
<!-- 表工具栏-->
<div id="cartonPageTable_tb" style="text-align: right;display: none">
    <a class="easyui-linkbutton" href="javascript:void(0);" iconCls="icon-add"
       onclick="javascript:$('#cartonPageTable').edatagrid('addRow',0)"
       plain="true">添加</a>
    <a class="easyui-linkbutton" href="javascript:void(0);" iconCls="icon-remove"
       onclick="javascript:$('#cartonPageTable').edatagrid('destroyRow')"
       plain="true">删除</a>
    <a class="easyui-linkbutton" href="javascript:void(0);" iconCls="icon-save"
       onclick="javascript:$('#cartonPageTable').edatagrid('saveRow')"
       plain="true">保存</a>
</div>
<script th:inline="none">
    //公司信息管理鼠标移动显示事件
    $('#cartonPageCompanyInfoManager').tooltip({
        content: $('<div></div>'),
        // showEvent: 'mousemove',
        showEvent: 'click',
        onUpdate: function(content){
            //获取信息管理：添加、修改、删除html内容
             var cartonPageCompanyOperation=$("#cartonPageCompanyOperation").html()
            content.panel({
                width: 100,
                border: false,
                title: '',
                content: cartonPageCompanyOperation
            });
        },
        onShow: function(){
            var t = $(this);
            t.tooltip('tip').unbind().bind('mouseenter', function(){
                t.tooltip('show');
            }).bind('mouseleave', function(){
                t.tooltip('hide');
            });
        }
    });

    //点击事件：添加公司信息
    function cartonPageCompanyAdd(){
        //建立单独的一个div，来动态加载窗口内容，后续使用jquery方法来清空此div(包括本div)的所有内容
        $("#cartonPage_company_dialog").append("<div id='companyAddDongtaiDiv'></div>")
        //动态的div已加载到浏览器内存中了，动态加载公司信息html内容
        $("#companyAddDongtaiDiv").append(cartonPageMoban());
        $("#companyAddDongtaiDiv").dialog({
            title:'添加公司信息',
            width:680,
            height:390,
            modal:true,
            content: $("#cartonPage_company_item").html(),
            buttons: [{
                text:'添加',
                iconCls:'icon-ok',
                handler:function(){
                    var serialize = $('#cartonPage_company_item_form').serialize();
                    var data=  commonAjax("post","/costReportCarton/addCartonCompanyInfo",serialize)
                    if(data.status==200){
                        $("#cartonPageCompanyNameCombobox").combobox("reload");
                        $("#companyAddDongtaiDiv").dialog("close")
                    }
                    layer.msg(data.msg)
                }
            }],
            onClose:function () {
                $("#companyAddDongtaiDiv").remove()  //清除浏览器中的html内容
            }
        })
    }

    //点击事件：修改公司信息
    function cartonPageCompanyUpdate(){
        var companyName=$("#cartonPageCompanyNameCombobox").combobox("getValue");
        if(!companyName){
            windowsOperation(null,null,{message:"请先选择公司！"})
            return
        }
        //建立单独的一个div，来动态加载窗口内容，后续使用jquery方法来清空此div(包括本div)的所有内容
        $("#cartonPage_company_dialog").append("<div id='companyAddDongtaiDiv'></div>")
        //动态的div已加载到浏览器内存中了，动态加载公司信息html内容
        $("#companyAddDongtaiDiv").append(cartonPageMoban());
        $("#companyAddDongtaiDiv").dialog({
            title:'添加公司信息',
            width:680,
            height:440,
            modal:true,
            content: $("#cartonPage_company_item").html(),
            buttons: [{
                text:'修改',
                iconCls:'icon-edit',
                handler:function(){
                    var serialize = $('#cartonPage_company_item_form').serialize();
                    var id=$("#cartonPageId").val();  //获取模板中的公司id
                    var companyName=$("#cartonPageCompanyName").val(); //获取模板中的公司名称
                    var data=  commonAjax("post","/costReportCarton/updateCartonCompanyInfo",serialize)
                    if(data.status==200){
                        $("#cartonPageCompanyNameCombobox").combobox("reload");
                        $("#companyAddDongtaiDiv").dialog("close")
                        $("#cartonPageCompanyNameCombobox").combobox("setValue",companyName);
                        //刷新数据:
                        var result= commonAjax("post","/costReportCarton/getCartonCompanyAndPrice",{id:id})
                        cartonPageTableInit(result);
                    }
                    layer.msg(data.msg)
                }
            }],
            onOpen:function(){
                //1、根据公司id获取后台相应的数据赋值到表单中  cartonPageQuotationCodeCombobox
                //查询该公司是否有多个报价单号：quotation_code,若为空或一个直接刷出数据该公司的信息数据
                var quotationCode= commonAjax("post","/costReportCarton/getCartonCompanyCompanyName",{companyName:companyName})
                if(quotationCode.length==1){
                    //2、显示公司详情，往表中赋值
                    cartonPageCompanyItemForm(quotationCode[0]);
                }else {
                    var id=$("#cartonPageQuotationCodeCombobox").combobox("getValue");
                    if(!id){
                        layer.msg("请选择报价单号！")
                        return
                    }
                    //通过公司id获取公司信息
                    var data= commonAjax("post","/costReportCarton/getCartonCompanyAndPrice",{id:id})
                    cartonPageCompanyItemForm(data.company);
                }

            },
            onClose:function () {
                $("#companyAddDongtaiDiv").remove()  //清除浏览器中的html内容
            }
        })
    }

    function cartonPageCompanyItemForm(data) {
        $('#cartonPage_company_item_form').form('load',{
            id:data.id,                                         //公司表中id，用于增删改
            quotationCode:data.quotationCode,                   //报价单
            quotationDate:data.quotationDate,                       //报价日期
            companyName:data.companyName,                           //公司名称
            companyAddress:data.companyAddress,                   //公司地址
            companyPhone:data.companyPhone,                     //电话号码
            companyFax:data.companyFax,                       //传真
            remark:data.remark,                                 //备注
            explain:data.explain,                          //说明
            quotationType:data.quotationType
        });
    }

    //点击事件：删除公司信息
    function cartonPageCompanyDelete(){
        var companyName=$("#cartonPageCompanyNameCombobox").combobox("getValue");
        if(!companyName){
            windowsOperation(null,null,{message:"请选择公司！"})
            return
        }
        //根据公司名称获取相对于的数据，有多个找用户选择的报价单号查询
        var quotationCode= commonAjax("post","/costReportCarton/getCartonCompanyCompanyName",{companyName:companyName})
        if(quotationCode.length==1){
            $.messager.confirm('确认对话框', '您确定要删除本公司的所有信息吗（包括价格数据）', function(r){
                if (r){
                    //根据id删除所有信息
                    var data=  commonAjax("post","/costReportCarton/deleteCartonCompanyInfo",{id:quotationCode[0].id})
                    if(data.status==200){
                        $("#cartonPageCompanyNameCombobox").combobox("reload");
                        $("#cartonPageCompanyNameCombobox").combobox("clear");
                        $("#cartonPageQuotationCodeCombobox").parent().hide()
                        $("#cartonPageTable").datagrid('loadData',[]);
                    }
                    layer.msg(data.msg)
                }
            });
        }else {
            //获取用户选择的报价单号：值为公司id
            var id=$("#cartonPageQuotationCodeCombobox").combobox("getValue");
            if(!id){
                windowsOperation(null,null,{message:"请选择报价单号！"})
                return
            }
            $.messager.confirm('确认对话框', '您确定要删除本公司的所有信息吗（包括价格数据）', function(r){
                if (r){
                    //根据id删除所有信息
                    var data=  commonAjax("post","/costReportCarton/deleteCartonCompanyInfo",{id:id})
                    if(data.status==200){
                        $("#cartonPageCompanyNameCombobox").combobox("reload");
                        $("#cartonPageCompanyNameCombobox").combobox("clear");
                        $("#cartonPageQuotationCodeCombobox").parent().hide()
                        $("#cartonPageTable").datagrid('loadData',[]);
                    }
                    layer.msg(data.msg)
                }
            });
        }

    }
    //初始化下拉列表Combobox
    $("#cartonPageCompanyNameCombobox").combobox({
        limitToList:true,
        editable:false,
        panelHeight:'auto',
        width:200,
        panelMaxHeight:200,
        url:"/costReportCarton/getCartonCompany",
        onChange:function (newValue,oldValue) {
            //查询该公司是否有多个报价单号：quotation_code,若为空或一个直接刷出数据该公司的信息数据，否则显示报价单号combobox组件让其选择
            var quotationCode= commonAjax("post","/costReportCarton/getCartonCompanyCompanyName",{companyName:newValue})
            if(quotationCode.length==1){
                $("#cartonPageQuotationCodeCombobox").parent().hide()
                var data= commonAjax("post","/costReportCarton/getCartonCompanyAndPrice",{id:quotationCode[0].id})
                cartonPageTableInit(data);
            }else if(quotationCode.length>1){
                //显示报价单号的combobox组件
                $("#cartonPageQuotationCodeCombobox").parent().show()
                $("#cartonPageQuotationCodeCombobox").combobox({
                    limitToList:true,
                    editable:false,
                    panelHeight:'auto',
                    width:150,
                    panelMaxHeight:200,
                    data:quotationCode,
                    onChange:function (newValue,oldValue) {
                       //通过id查询相应的数据
                        var data= commonAjax("post","/costReportCarton/getCartonCompanyAndPrice",{id:newValue})
                        cartonPageTableInit(data);
                    }
                })
            }

        }

    })

    function cartonPageTableInit(data) {
        //表
        //给表的父类div赋值宽度：进行屏幕适配
        var cartonPageTableWidth= costsystemFblW*1.1//获取定义好的宽度乘以110%
        $("#cartonPageTable").parent().css({width:cartonPageTableWidth})
        //初始化表格
        $("#cartonPageTable").edatagrid({
            data:data.cp,
            nowrap:false,
            fitColumns:true,
            showFooter:true,
            autoSave:true,
            idField:'id',
            columns:[
                [{field:'companyName',title:data.company.companyName,colspan:10,align:'center'}],
                [
                    {field:'quotationCode1',title:'报价单',align:'center'},
                    {field:'quotationCode',title:data.company.quotationCode,colspan:3},
                    {field:'quotationDate1',title:'报价日期',align:'center'},
                    {field:'quotationDate',title:data.company.quotationDate,colspan:2},
                    {field:'quotationType1',title:'类型',align:'center'},
                    {field:'quotationType',title:data.company.quotationType,colspan:2}
                ],
                [
                    {field:'companyPhone1',title:'公司电话',align:'center'},
                    {field:'companyPhone',title:data.company.companyPhone,colspan:3},
                    {field:'companyFax1',title:'公司传真',align:'center'},
                    {field:'companyFax',title:data.company.companyFax,colspan:5}
                ],
                [
                    {field:'companyAddress1',title:'公司地址',align:'center'},
                    {field:'companyAddress',title:data.company.companyAddress,colspan:9}

                ],
                [
                    {field:'id',title:'主键ID',hidden:true},
                    {field:'num',title:'项目序号',width:50,align:'center',editor:{type:'numberbox',options:{required:true}}},
                    {field:'code',title:'纸板型号',width:70,align:'center',editor:{type:'textbox',options:{required:true}}},
                    {field:'cd1',title:'材质',width:100,align:'center',editor:{type:'textbox',options:{}}},
                    {field:'cd2',title:'耐破值',width:50,align:'center',editor:{type:'textbox',options:{}}},
                    {field:'cd3',title:'边压值',width:50,align:'center',editor:{type:'textbox',options:{}}},
                    {field:'cd4',title:'含水率',width:50,align:'center',editor:{type:'textbox',options:{}}},
                    {field:'noPayPrice',title:'未税单价',width:50,align:'center',editor:{type:'numberbox',options:{precision:4}}},
                    {field:'payPrice',title:'含税单价',width:50,align:'center',editor:{type:'numberbox',options:{precision:4}}},
                    {field:'payPrice98',title:'98折含税单价',width:70,align:'center',editor:{type:'numberbox',options:{precision:4}}},
                    {field:'companyId',title:'纸箱公司详情id',hidden:true,editor:{type:'numberbox'}}

                ]
            ],
            saveUrl:"/costReportCarton/saveCartonPrice?companyId="+data.company.id,
            updateUrl:"/costReportCarton/updateCartonPrice",
            destroyUrl:"/costReportCarton/deleteCartonPrice",
            onSave:function (index,row) {
                if (row.status){
                    layer.msg(row.msg)
                }
            },
            onDestroy:function(index,row){
                if (row.status){
                    layer.msg(row.msg)
                }
            },
            onDestroy:function(index,row){
                if (row.status){
                    layer.msg(row.msg)
                }
            }
        })
        $("#labelRemark").html('<span style="font-size: 22px"><span style="color: #CC2222">备注：</span>'+data.company.remark+'</span>')
        $("#labelExplain").html('<span style="font-size: 22px"><span style="color: #3c8b3c">说明：</span>'+data.company.explain+'</span>')
    }

    //模板：<!--公司信息管理内容,需要进行动态加载，不然会造成有重复的，因为easyui会对原始的组件进行初始化造成重复-->
    function cartonPageMoban() {
        return '<div id="cartonPage_company_item" style="text-align: center;display: none">\n' +
            '    <div class="cartonPage-item-info">\n' +
            '        <div class="demo-tip icon-tip"></div>\n' +
            '        <div>\n' +
            '            <form id="cartonPage_company_item_form">\n' +
            '                <div>\n' +
            '                公司名称\n' +
            '                <input id="cartonPageCompanyName" class="easyui-textbox" name="companyName" data-options="" />\n' +
            '                <input id="cartonPageId"  name="id" style="display: none"/>\n' +
            '                报价单号\n' +
            '                <input id="cartonPageQuotationCode" class="easyui-textbox" name="quotationCode" data-options="" />\n' +
            '                报价日期\n' +
            '                <input id="cartonPageQuotationDate" class="easyui-datebox" name="quotationDate" data-options="" />\n' +
            '                </div>\n' +
            '                <div style="margin-top: 20px">\n' +
            '                纸箱类型\n' +
            '                <input id="cartonPageQuotationType" class="easyui-textbox" name="quotationType" data-options="" />\n' +
            '                公司电话\n' +
            '                <input id="cartonPageCompanyPhone" class="easyui-textbox" name="companyPhone" data-options="" />\n' +
            '                公司传真\n' +
            '                <input id="cartonPageCompanyFax" class="easyui-textbox" name="companyFax" data-options="" />\n' +
            '                </div>\n' +
            '               <div style="margin-top: 20px">\n' +
            '               公司地址\n' +
            '               <input id="cartonPageCompanyAddress" class="easyui-textbox" name="companyAddress" data-options="width:560" />\n' +
            '               </div>\n' +
            '                <div style="margin-top: 20px;margin-left: 25px">\n' +
            '                备注\n' +
            '                <input id="cartonPageRemark" class="easyui-textbox" name="remark" data-options="multiline:true,height:80,width:560" />\n' +
            '                </div>\n' +
            '                <div style="margin-top: 20px;margin-left: 25px">\n' +
            '                说明\n' +
            '                <input id="cartonPageExplain" class="easyui-textbox" name="explain" data-options="multiline:true,height:80,width:560" />\n' +
            '                </div>\n' +
            '            </form>\n' +
            '        </div>\n' +
            '    </div>\n' +
            '</div>'
    }
</script>